package sh.wnxy.wn38.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sh.wnxy.wn38.entity.User;
import sh.wnxy.wn38.service.IUserService;
import sh.wnxy.wn38.service.vo.LoginForm;
import sh.wnxy.wn38.service.vo.usc;
import sh.wnxy.wn38.utils.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-14
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    private IUserService us;

    @Autowired
    public void setUserroleService(IUserService us) {
        this.us = us;
    }

    @PostMapping("/login")
    public Result denglu(@RequestBody usc user) {
        QueryWrapper<User> ac = new QueryWrapper();
        if (!StringUtils.isEmpty(user.getUserName())) {
            ac.eq("user_name", user.getUserName());
        }
        if (!StringUtils.isEmpty(user.getUserPwd())) {
            ac.eq("user_pwd", user.getUserPwd());
        }
        User one = us.getOne(ac);
        return one != null ? Result.ok().msg("登陆成功").data("user",one) : Result.error().msg("用户名或密码错误");
    }


    @PostMapping("/zhuce")
    public Result zhuce(@RequestBody LoginForm loginForm) {
        QueryWrapper<User> ac = new QueryWrapper();
        if (!StringUtils.isEmpty(loginForm.getUserName())) {
            ac.eq("user_name", loginForm.getUserName());
            if (!(us.getOne(ac) == null)) {
                return Result.error().msg("用户名已存在");
            }
        }
        User user = new User();
        BeanUtils.copyProperties(loginForm, user);
        us.save(user);
        return Result.ok().msg("注册成功");
    }

    @PostMapping("/xiugai/{userId}")
    public Result xiugai(@RequestBody LoginForm loginForm,@PathVariable("userId") String userId) {
        User user = new User();
        BeanUtils.copyProperties(loginForm, user);
        System.out.println(userId);
        user.setUserId(userId);
        return us.updateById(user) ? Result.ok().msg("修改成功") : Result.error();
    }


    @ApiOperation(value = "更新用户信息")
    @PostMapping("/updateUser")
    public Result updateTeacherById(@ApiParam("新的讲师信息") @RequestBody User user) {
        boolean result = us.updateById(user);
        if (result) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/xiugaihuixian/{userId}")
    public Result xiugaihuixian (@PathVariable("userId") String userId){
//        String UserId="1416219626060218370";
        User byId = us.getById(userId);
        System.out.println(byId);
        return Result.ok().data("byId",byId);
    }

    @PostMapping("")
    public Result register() {
        return null;
    }

    @GetMapping("/findUserById/{id}")
    public Result findUserById(@PathVariable("id") String userId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", userId);
        User one = us.getOne(userQueryWrapper);
        return one != null ? Result.ok().data("user", one) : Result.error();
    }
    @PostMapping("/guanlidenglu")
    public Result guanliyuandenglu(@RequestBody usc Use) {
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(Use.getUserPwd())){
            Wrapper.eq("user_pwd",Use.getUserPwd());
        }
        if(StringUtils.isEmpty(Use.getUserName())){
            Wrapper.eq("user_name",Use.getUserName());
        }
        User one = us.getOne(Wrapper);
        if(one==null){
            return Result.error().msg("用户名或密码错误");
        }

        System.out.println(Use);
        return  Result.ok();
    }


}

